The mode of a list of numbers is the number that appears most frequently.
from collections import Counter
def calculate_mode(numbers):
counts = Counter(numbers)
max_count = max(counts.values())
mode = [number for number, count in counts.items() if count == max_count]
return mode
# Taking input for numbers and calculating the mode
def calculate_and_display_mode():
numbers = [float(x) for x in input("Enter numbers separated by spaces: ").split()]
mode = calculate_mode(numbers)
print("Mode of the numbers:", mode)
calculate_and_display_mode()
Enter numbers separated by spaces: 3 5 1 5 7 5
Mode of the numbers: [5.0]
Enter numbers separated by spaces: 2 6 4 8 10 12 2 8
Mode of the numbers: [2.0, 8.0]
The function calculate_mode(numbers)
calculates the mode of a list of numbers using the Counter
class from the collections
module.
The function calculate_and_display_mode()
takes input for numbers, calculates the mode using the aforementioned function, and displays the result.